//
//  HomeViewController.swift
//  DSWeibo
//
//  Created by J on 16/2/21.
//  Copyright © 2016年 J. All rights reserved.
//

import UIKit
import SVProgressHUD
let XMGHomeRegister = "XMGHomeRegister"
class HomeTableViewController: BaseTableViewController {

        /// 保存微博数组
    var statuses: [Statuses]?{
        didSet{
            
            //设置完毕数据 刷新表格
            tableView.reloadData()
        }
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: XMGHomeRegister)
        
        //1.如果没有登陆,就设置未登陆界面的信息
        if !userLogin{
            visitorView?.setUpVisitorView(true, imageName: "visitordiscover_feed_image_house", message: "关注一些人，回这里看看有什么惊喜")
        }
        
        //2.初始化导航条
        setUpNav()
        
        //注册通知监听菜单
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(HomeTableViewController.change), name: XMGPopoverAnimationWillShow, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(HomeTableViewController.change), name: XMGPopoverAnimationWillDismiss, object: nil)
        //4.加载微博数据
            loadData()
        
    }
    
    //修改标题按钮状态
    func change(){
        
        let titlButton = navigationItem.titleView as!TitleButton
        titlButton.selected = !titlButton.selected
    }
    
    
    
    
    //移除通知 对应dealloc
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

  
    private func setUpNav(){
    //1.初始化左右按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem.createBarButtonItem("navigationbar_friendattention", target: self, action: #selector(HomeTableViewController.leftButtonClick))
        navigationItem.rightBarButtonItem = UIBarButtonItem.createBarButtonItem("navigationbar_pop", target: self, action: #selector(HomeTableViewController.rightButtonClick))
        
    //2.初始化标题按钮
    
        let titleBtn = TitleButton()
        navigationItem.titleView = titleBtn
        titleBtn.addTarget(self, action: #selector(HomeTableViewController.TitleButtonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)

        
    }
    
    func loadData(){
        Statuses.loadStatuses { (models, error) in
            if error != nil{//如果有错 
                print(error)
                return
            }
            self.statuses = models
 
        }
        
    }
    
    
    func rightButtonClick(){
        
        let sb = UIStoryboard(name: "QRCodeViewController", bundle: nil)
        
        let vc = sb.instantiateInitialViewController()!
        presentViewController(vc, animated: true, completion: nil)
    }
    
    func leftButtonClick(){
        
        
    }
    ///标题按钮点击
    func TitleButtonClick(btn:TitleButton){
        
        //1.修改箭头方向
//        btn.selected = !btn.selected
        //2.弹出菜单
        let sb = UIStoryboard(name: "PopoverViewController", bundle: nil)
        let vc = sb.instantiateInitialViewController()
        
        //2.1设置转场代理
        //默认情况下modal会移除之前控制器的view 替换为当前弹出的view
        //如果自定义转场,那么就不会移除之前控制器的view
        vc?.transitioningDelegate = popoverAnimator
        vc?.modalPresentationStyle = UIModalPresentationStyle.Custom
      
        //2.2设置转场的样式
        presentViewController(vc!, animated: true, completion: nil)
    }
    

    
    //MARK:懒加载
    //以ID那个要定义一个属性来保存自定义转场对象 否则会报错
    private lazy var popoverAnimator : PopoverAnimation = {
        
        let pa = PopoverAnimation()
        pa.presentFrame =  CGRect(x:(UIScreen.mainScreen().bounds.width - 200)*0.5,y:60, width: 200, height: 200)
        return pa
        
    }()
    
}


extension  HomeTableViewController{
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuses?.count ?? 0
    }
    
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(XMGHomeRegister)
        
        let status = statuses![indexPath.row]
        cell?.textLabel?.text = status.text
        
        return cell!
    }
    
}

